//公司编辑公司
import React, { Component } from 'react'
import {
    View,
    Text,
    TextInput,
    TouchableOpacity,
    ScrollView,
} from 'react-native';

import { Cpublishprobody_Style as styles } from '../../../../styles';
import Icon from 'react-native-vector-icons/Ionicons';

import { Edit_CompanyAction } from '../../../actions/postion_c'
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { myAlert } from '../../../util/compatible'
let Dimensions = require('Dimensions');
let width = Dimensions.get('window').width;
import ChooseUtil from '../../../util/chooseUtil';

class Ccombasic_body extends Component {
    constructor(props) {
        super(props);
        this.state = {
            company: {},
        }
    };

    //加载后渲染页面
    componentDidMount() {
        if (!this.state.company.companyId && this.props.company.companyId) {
            this.setState({ company: this.props.company })
        }
    }

    componentWillReceiveProps(nextProps) {
        const { data } = nextProps.msg
        if (data.msg != "") {
            myAlert(data.msg);
        }
    }

    render() {
        return (
            <ScrollView style={[styles.pub_all, { width: width * 1 }]}>

                <View style={styles.pub_eve1}>

                    <View style={styles.pub_sma2}>
                        <Text style={styles.pub_txtname}>公司名称</Text>
                        <TextInput
                            style={[styles.inputname, { width: width * 0.7 }]}
                            underlineColorAndroid="transparent"
                            defaultValue={this.state.company.companyName}
                            onChangeText={this.companyName.bind(this)}
                        />
                    </View>

                    <View style={styles.pub_sma2}>
                        <Text style={styles.pub_txtname}>公司规模 </Text>
                        <TouchableOpacity style={[styles.pub_touch1, { width: width * 0.7 }]}
                            onPress={() => { this.props.navigation.navigate('Companysize_all', { 'rollback': this.companyScale.bind(this) }) }}>
                            <Text style={styles.pub_txt2}>{this.state.company.companyScale}</Text>
                        </TouchableOpacity>
                    </View>

                    <View style={styles.pub_sma2}>
                        <Text style={styles.pub_txtname}>公司性质</Text>
                        <ChooseUtil ref="chooseUtil" rollback={this.companyNature.bind(this)} />
                        <TouchableOpacity style={[styles.pub_touch1, { width: width * 0.7 }]}
                            onPress={() => { this.show3() }}>
                            <Text style={styles.pub_txt2}>
                                {this.nature(this.state.company.companyNature)}
                            </Text>
                        </TouchableOpacity>
                    </View>
                </View>

                <View style={styles.pub_eve3}>
                    <View style={styles.pub_sma2}>
                        <Text style={styles.pub_txtname}>选择行业</Text>
                        <TouchableOpacity
                            style={[styles.pub_touch1, { width: width * 0.7 }]}
                            onPress={() => {
                                this.props.navigation.navigate('Trade', { Type: 1, 'rollback': this.setHy.bind(this), list: this.state.company.fields })
                            }}>
                            <Text style={styles.pub_txt2}>
                                {this.fieldName(this.state.company.fields, 3)}
                            </Text>
                        </TouchableOpacity>
                    </View>
                    <View style={styles.pub_sma2}>
                        <Text style={styles.pub_txtname}>公司地点</Text>
                        {/* 传入公司经纬度 */}
                        <TouchableOpacity
                            style={[styles.pub_touch1, { width: width * 0.7 }]}
                            onPress={() => { this.props.navigation.navigate('Map_all_c', { 'rollback': this.companyLocation.bind(this), companyY: this.state.company.companyY, companyX: this.state.company.companyX }) }}>
                            <Text style={styles.pub_txt2}>{this.state.company.companyLocation ? this.state.company.companyLocation : '选择'}</Text>
                        </TouchableOpacity>
                    </View>
                    <View style={styles.pub_sma2}>
                        <Text style={styles.pub_txtname}>福利待遇</Text>
                        <TouchableOpacity onPress={() => { this.props.navigation.navigate('Cbenefits_all', { 'rollback': this.welfares.bind(this), list: this.state.company.welfares }) }}
                            style={[styles.pub_touch1, { width: width * 0.7 }]}>
                            <Text style={styles.pub_txt2}>{this.welfareName(this.state.company.welfares, 3)}</Text>
                        </TouchableOpacity>
                    </View>
                </View>

                <TouchableOpacity
                    onPress={() => {
                        this.props.navigation.navigate('Cdescribe_all', { 'rollback': this.companyIntro.bind(this), text: this.state.company.companyIntro })
                    }}
                    style={styles.pub_eve4}>
                    <View style={{ height: 119, overflow: 'hidden', backgroundColor: '#ffffff' }}>
                        <Text style={styles.pub_txt3}>公司简介</Text>
                        <Text style={styles.pub_txt3}>{this.state.company.companyIntro ? this.state.company.companyIntro : '点击输入公司简介'}</Text>
                    </View>
                    <Icon 
                        name={'ios-create'}
                        size={20}
                        color={'#3d9ccc'} style={styles.pub_icon3} />
                </TouchableOpacity>

                <Text style={styles.pub_txt4}>请认真填写公司信息，以提高职位通过率</Text>

                <View style={styles.pub_tb}>
                    <TouchableOpacity
                        activeOpacity={1}
                        style={[styles.pub_button, { width: width * 0.8 }]}
                        onPress={() => {
                            const c = {
                                companyId: this.state.company.companyId,
                                companyName: this.state.company.companyName,
                                companyScale: this.state.company.companyScale,
                                companyNature: this.state.company.companyNature,
                                fields: this.state.company.fields,
                                welfares: this.state.company.welfares,
                                companyIntro: this.state.company.companyIntro,
                                companyLocation: this.state.company.companyLocation,
                                companyX: this.state.company.companyX,
                                companyY: this.state.company.companyY
                            }
                            this.props.actions.Edit_CompanyAction(c);
                        }}>
                        <Text style={styles.pub_txt5}>保存</Text>
                    </TouchableOpacity>
                </View>
            </ScrollView >
        )
    }

    // 教育
    show() {
        this.refs.chooseUtil.initPicker("education");
        this.refs.chooseUtil.picker.show();
        this.refs.chooseUtil.setState({
            isModal: true
        })
    }
    // 经验
    show2() {
        this.refs.chooseUtil.initPicker("experience");
        this.refs.chooseUtil.picker.show();
        this.refs.chooseUtil.setState({
            isModal: true
        })
    }
    //公司
    show3() {
        this.refs.chooseUtil.initPicker("company");
        this.refs.chooseUtil.picker.show();
        this.refs.chooseUtil.setState({
            isModal: true
        })
    }

    show4() {
        this.refs.chooseUtil.initPicker("state");
        this.refs.chooseUtil.picker.show();
        this.refs.chooseUtil.setState({
            isModal: true
        })
    }

    go(key) {
        //时间插件确认回调
        const publish = this.state.publish;
        publish.publishState = key;
        this.setState({ publish: publish });
    }

    //公司名称
    companyName(a) {
        const company = this.state.company;
        company.companyName = a;
        this.setState({ company: company });
    }

    //公司规模
    companyScale(a) {
        const company = this.state.company;
        company.companyScale = a;
        this.setState({ company: company });
    }

    //公司性质
    companyNature(a) {
        let n = 0;
        if (a.data == '国企') {
            n = 1;
        } else if (a.data == '民营') {
            n = 2;
        } else if (a.data == '外企') {
            n = 3;
        } else if (a.data == '政府') {
            n = 4;
        }
        const company = this.state.company;
        company.companyNature = n;
        this.setState({ company: company });
    }
    nature(a) {
        let n = '';
        if (a == 1) {
            n = '国企';
        } else if (a == 2) {
            n = '民营';
        } else if (a == 3) {
            n = '外企';
        } else if (a == 4) {
            n = '政府';
        } else {
            n = '选择'
        }
        return n;
    }

    //选择行业
    setHy(val, type) {
        const company = this.state.company;
        company.fields = val;
        this.setState({
            company: company
        })
    }

    //经营领域
    fieldName(List, sl) {
        let arr = '';
        if (List != undefined) {
            let s = 1;
            for (let i = 0; i < List.length; i++) {
                if (s <= sl) {
                    arr += List[i].fieldName + '/';
                    ++s;
                }
            }
            if (arr.endsWith('/')) {
                arr = arr.substr(0, arr.length - 1);
            }
        }
        return arr;
    }

    //福利待遇显示
    welfareName(List, sl) {
        let arr = '';
        if (List != undefined) {
            let s = 1;
            for (let i = 0; i < List.length; i++) {
                if (s <= sl) {
                    arr += List[i].welfareName + '/';
                    ++s;
                }
            }
            if (arr.endsWith('/')) {
                arr = arr.substr(0, arr.length - 1);
            }
        }
        return arr;
    }

    //福利待遇
    welfares(a) {
        const company = this.state.company;
        company.welfares = a;
        this.setState({ company: company });
    }
    //公司简介
    companyIntro(a) {
        const company = this.state.company;
        company.companyIntro = a;
        this.setState({ company: company });
    }

    companyLocation(a) {
        const company = this.state.company;
        company.companyLocation = a.address;
        company.companyX = a.longitude;
        company.companyY = a.latitude;
        this.setState({ company: company });
    }
}

//组合action
var actions = {
    Edit_CompanyAction,
}
//连接state到props
const mapStateToProps = state => {
    return {
        msg: state.Edit_CompanyReducer             //自己的reducers
    }
}
//连接action
const mapDispatchToProps = (dispatch) => {
    return {
        actions: bindActionCreators(actions, dispatch)
    }
}
//导出组合好的组件(组合后状态改变重新渲染组合组件的页面)
export default connect(
    mapStateToProps, mapDispatchToProps
)(Ccombasic_body);